package cn.com.czpb.client.module.news.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import cn.com.czpb.client.R;
import cn.com.czpb.client.app.App;
import cn.com.czpb.client.app.BaseFragment;
import cn.com.czpb.client.model.base.BaseEntity;
import cn.com.czpb.client.model.subscribe.SubscribeListSecondBean;
import cn.com.czpb.client.model.subscribe.SubscribeListSecondEntity;
import cn.com.czpb.client.module.news.listener.RefreshDataListener;
import cn.com.czpb.client.module.subscribe.adapter.SubscribeMoreListAdapter;
import cn.com.czpb.client.net.Factory;
import cn.com.czpb.client.utils.DeviceUtils;
import cn.com.czpb.client.utils.LogUtils;
import cn.com.czpb.client.utils.PageCtrl;
import cn.com.czpb.client.utils.SPUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by 猪猪侠 on 2017/8/9.
 */

public class SearchSubscribeFragment extends BaseFragment implements RefreshDataListener, BaseQuickAdapter.OnItemChildClickListener {
    @BindView(R.id.rc_searchnews_id)
    RecyclerView rcSearchnewsId;
    private SubscribeMoreListAdapter adapter;
    private String con;

    @Override
    public int setMyContentView() {
        return R.layout.fragment_searchsubscribe_layout;
    }

    @Override
    public void initView() {

        LinearLayoutManager manager = new LinearLayoutManager(activity);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        rcSearchnewsId.setLayoutManager(manager);
        adapter = new SubscribeMoreListAdapter(null);
        adapter.setOnItemChildClickListener(this);
        rcSearchnewsId.setAdapter(adapter);
    }

    @Override
    public void initData() {

    }

    @Override
    public void refreshData(String con) {
        this.con = con;
        writerSearch(con);

    }

    /**
     * 达人号搜索
     * @param con
     */
    private void writerSearch(String con) {
        spu= SPUtil.getInstance();
        HashMap<String,String> map = new HashMap<>();
        map.put("nicknameortype",con);
        map.put("device",DeviceUtils.getMyUUID(App.getContext()));
        if (spu.getUser()!=null){
            map.put("uid",spu.getUser().getUid());
        }
        Factory.provideHttpService().writerSearch(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<SubscribeListSecondEntity>() {
                    @Override
                    public void call(SubscribeListSecondEntity subscribeListSecondEntity) {
                        setData(subscribeListSecondEntity.data);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {

                    }
                });
    }

    /**
     *
     * @param data
     */
    private void setData(List<SubscribeListSecondBean> data) {
        adapter.setNewData(data);
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        SubscribeListSecondBean bean = (SubscribeListSecondBean) adapter.getItem(position);
        switch (view.getId()){
            case R.id.img_subscribe_id:
                //订阅
                LogUtils.i("订阅");
                focusUser(bean.getUid());
                break;
            case R.id.img_remove_subscribe_id:
                //订阅
                LogUtils.i("取消订阅");
                removeFocusUser(bean.getUid());
                break;
            case R.id.ll_root_id:
                //作者个人主页
                PageCtrl.start2SubscribeDetailActivity(activity,bean.getUid(),"");
                break;
        }
    }

    //订阅达人号
    private void focusUser(String uid){
        Map<String ,String> map = new HashMap<>();
        if (spu.getUser()!=null){
            map.put("uid",spu.getUser().getUid());
        }
        map.put("device", DeviceUtils.getMyUUID(activity));
        map.put("focusid",uid);
        Factory.provideHttpService().focusUser(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<BaseEntity>() {
                    @Override
                    public void call(BaseEntity baseEntity) {
                        writerSearch(con);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {

                    }
                });

    }
    //取消订阅
    private void removeFocusUser(String uid){
        Map<String ,String> map = new HashMap<>();
        if (spu.getUser()!=null){
            map.put("uid",spu.getUser().getUid());
        }
        map.put("device",DeviceUtils.getMyUUID(activity));
        map.put("focusid",uid);
        Factory.provideHttpService().removeFocusUser(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<BaseEntity>() {
                    @Override
                    public void call(BaseEntity baseEntity) {
                        writerSearch(con);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {

                    }
                });

    }
}
